﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Jzy.BLL.Project;
using Jzy.Model.Project;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Attach;


public partial class Project_Attach_List : System.Web.UI.Page
{
    private string TaskID
    {
        get
        {
            if (Request["TaskID"] != null)
            {
                return StrUtil.DecryptUrl(Request["TaskID"]);
            }
            else
            {
                return null;
            }
        }
    }

    private string PrjID
    {
        get
        {
            if (Request.QueryString["PrjID"] != null)
            {
                return StrUtil.DecryptUrl(Request.QueryString["PrjID"]);
            }
            else
            {
                return null;
            }
        }
    }

    private ProjectInfo _prjInfo;
    private ProjectInfo PrjInfo
    {
        get
        {
            if (_prjInfo == null)
            {
                _prjInfo = (new ProjectBO()).GetInfo(PrjID);
            }
            return _prjInfo;
        }

    }

    protected bool DisplayDeleteButton(object userID)
    {
        bool isDisplay = false;
        if (AppSession.UserID == PrjInfo.PMID || AppSession.UserID == Convert.ToInt32(userID))
        {
            isDisplay = true;
        }
        return isDisplay;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindView();
        }
    }

    private void BindView()
    {
        AttachBO attach = new AttachBO("PrjTaskAttach");
        IEnumerable<AttachInfo> list = null;
        if (TaskID != null)
        {
            list = attach.GetList(int.Parse(TaskID));
        }
        else
        {
            ProjectBO prj = new ProjectBO();
            List<int> taskIDs = prj.GetAllTaskID(PrjID);
            list = attach.GetList(taskIDs);
        }
        lv.DataSource = list;
        lv.DataBind();
    }

    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            HyperLink lnkDelete = (HyperLink)e.Item.FindControl("lnkDelete");

            AttachInfo drv = (AttachInfo)((ListViewDataItem)e.Item).DataItem;
            int userID = drv.CreatorID??0;
            int id = drv.ID;
            lnkDelete.Visible = DisplayDeleteButton(userID);

            string url = "Delete.ashx?PrjID=" + StrUtil.EncryptUrl(PrjID);
            if (TaskID != null)
            {
                url += "&TaskID=" + StrUtil.EncryptUrl(TaskID);
            }
            url += "&ID=" + StrUtil.EncryptUrl(id);

            lnkDelete.NavigateUrl = url;
            lnkDelete.Attributes.Add("onclick", "return confirm('确认删除?');");

        }
    }
}
